#include "usarttoolwidget.h"
#include "./ui_usarttoolwidget.h"

UsartToolWidget::UsartToolWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::UsartToolWidget)
{
    ui->setupUi(this);

    main = new QGroupBox(this);
    //容器位置设置初始化
    qh_box = new QHBoxLayout;
    qv_box = new QVBoxLayout;

    //容器初始化
    usartBox = new QGroupBox(QString("串口设置"),this);
    recvBox = new QGroupBox(QString("接受区设置"),this);
    sendBox = new QGroupBox(QString("发送区设置"),this);
    msgBox = new QGroupBox(QString("串口数据接收"),this);


    /*=========================================串口设置========================================*/
    qh_port = new QHBoxLayout;
    qh_bt = new QHBoxLayout;
    qh_jy = new QHBoxLayout;
    qh_data = new QHBoxLayout;
    qh_stop = new QHBoxLayout;
    usart = new QVBoxLayout;
    open = new QPushButton(QString("打开"), this);

    menu1 = new QMenu(this);
    QLabel *lab1 = new QLabel(QString("串口号"), this);
    QAction *m1_act1 = new QAction(QString("COM5"), this);
    QAction *m1_act2 = new QAction(QString("COM6"), this);
    QPushButton *but1 = new QPushButton(this);
    menu1->addAction(m1_act1);
    menu1->addAction(m1_act2);
    but1->setMenu(menu1);
    qh_port->addWidget(lab1);
    qh_port->addWidget(but1);
    //usartBox->setLayout(qh_port);


    menu2 = new QMenu( this);
    QLabel *lab2 = new QLabel(QString("波特率"), this);
    QAction *m2_act1 = new QAction(QString("9600"), this);
    QAction *m2_act2 = new QAction(QString("115200"), this);
    QPushButton *but2 = new QPushButton(this);
    menu2->addAction(m2_act1);
    menu2->addAction(m2_act2);
    but2->setMenu(menu2);
    qh_bt->addWidget(lab2);
    qh_bt->addWidget(but2);
    //usartBox->setLayout(qh_bt);

    menu3 = new QMenu( this);
    QLabel *lab3 = new QLabel(QString("校验位"), this);
    QAction *m3_act1 = new QAction(QString("NONE"), this);
    QAction *m3_act2 = new QAction(QString("1"), this);
    QPushButton *but3 = new QPushButton(this);
    menu3->addAction(m3_act1);
    menu3->addAction(m3_act2);
    but3->setMenu(menu3);
    qh_jy->addWidget(lab3);
    qh_jy->addWidget(but3);
    //usartBox->setLayout(qh_jy);

    menu4 = new QMenu( this);
    QLabel *lab4 = new QLabel(QString("数据位"), this);
    QAction *m4_act1 = new QAction(QString("8"), this);
    QAction *m4_act2 = new QAction(QString("7"), this);
    QPushButton *but4 = new QPushButton(this);
    menu4->addAction(m4_act1);
    menu4->addAction(m4_act2);
    but4->setMenu(menu4);
    qh_data->addWidget(lab4);
    qh_data->addWidget(but4);
    //usartBox->setLayout(qh_data);

    menu5 = new QMenu( this);
    QLabel *lab5 = new QLabel(QString("停止位"), this);
    QAction *m5_act1 = new QAction(QString("1"), this);
    QAction *m5_act2 = new QAction(QString("2"), this);
    QPushButton *but5 = new QPushButton(this);
    menu5->addAction(m5_act1);
    menu5->addAction(m5_act2);
    but5->setMenu(menu5);
    qh_stop->addWidget(lab5);
    qh_stop->addWidget(but5);
    //usartBox->setLayout(qh_stop);

    //串口垂直分布
    usart->addLayout(qh_port);
    usart->addLayout(qh_bt);
    usart->addLayout(qh_jy);
    usart->addLayout(qh_data);
    usart->addLayout(qh_stop);
    usart->addWidget(open);

    //将规划好的部件加入容器
    usartBox->setLayout(usart);
    // 选中菜单项后更新按钮文本
    connect(menu1, &QMenu::triggered, this, [but1](QAction *action) {
        but1->setText(action->text());  // 将选中的文本设置到按钮
    });
    connect(menu2, &QMenu::triggered, this, [but2](QAction *action) {
        but2->setText(action->text());  // 将选中的文本设置到按钮
    });
    connect(menu3, &QMenu::triggered, this, [but3](QAction *action) {
        but3->setText(action->text());  // 将选中的文本设置到按钮
    });
    connect(menu4, &QMenu::triggered, this, [but4](QAction *action) {
        but4->setText(action->text());  // 将选中的文本设置到按钮
    });
    connect(menu5, &QMenu::triggered, this, [but5](QAction *action) {
        but5->setText(action->text());  // 将选中的文本设置到按钮
    });



    /*=========================================接收区设置========================================*/
    qh_but = new QHBoxLayout;
    qv_re = new QVBoxLayout;

    cb1 = new QCheckBox(QString("接收转向文件"),this);
    cb2 = new QCheckBox(QString("显示接收时间"), this);
    cb3 = new QCheckBox(QString("十六进制显示"), this);
    cb4 = new QCheckBox(QString("暂停接收显示"), this);
    saveData = new QPushButton(QString("保存数据"), this);
    saveData->setStyleSheet(
        "QPushButton {"
        "   border: none;"                     // 去掉边框
        "   color: blue;"                      // 设置字体颜色为红色
        "   text-decoration: underline;"      // 添加下划线
        "}"
        "QPushButton:hover {"
        "   color: darkred;"                  // 鼠标悬停时的颜色
        "}"
        );
    clearShow = new QPushButton(QString("清除显示"), this);
    clearShow->setStyleSheet(
        "QPushButton {"
        "   border: none;"                     // 去掉边框
        "   color: blue;"                      // 设置字体颜色为红色
        "   text-decoration: underline;"      // 添加下划线
        "}"
        "QPushButton:hover {"
        "   color: darkred;"                  // 鼠标悬停时的颜色
        "}"
        );

    qh_but->addWidget(saveData);
    qh_but->addWidget(clearShow);

    qv_re->addWidget(cb1);
    qv_re->addWidget(cb2);
    qv_re->addWidget(cb3);
    qv_re->addWidget(cb4);
    qv_re->addLayout(qh_but);

    //将规划好的部件加入容器
    recvBox->setLayout(qv_re);


    /*=========================================发送区设置========================================*/
    qh_sendBut = new QHBoxLayout;
    qh_lab = new QHBoxLayout;
    qv_send = new QVBoxLayout;


    cb5 = new QCheckBox(QString("启用文件数据源"),this);
    cb6 = new QCheckBox(QString("自动发送附加位"), this);
    cb7 = new QCheckBox(QString("发送完自动清空"), this);
    cb8 = new QCheckBox(QString("按十六进制发送"), this);
    cb9 = new QCheckBox(QString("数据流循环发送"), this);
    send_line = new QLineEdit(this);
    send_lab1 = new QLabel(QString("发送间隔"), this);
    send_lab2 = new QLabel(QString("毫秒"), this);

    file_in = new QPushButton(QString("文件载入"), this);
    file_in->setStyleSheet(
        "QPushButton {"
        "   border: none;"                     // 去掉边框
        "   color: blue;"                      // 设置字体颜色为蓝色
        "   text-decoration: underline;"      // 添加下划线
        "}"
        "QPushButton:hover {"
        "   color: darkred;"                  // 鼠标悬停时的颜色
        "}"
        );
    file_clear = new QPushButton(QString("清除输入"), this);
    file_clear->setStyleSheet(
        "QPushButton {"
        "   border: none;"                     // 去掉边框
        "   color: blue;"                      // 设置字体颜色为蓝色
        "   text-decoration: underline;"      // 添加下划线
        "}"
        "QPushButton:hover {"
        "   color: darkred;"                  // 鼠标悬停时的颜色
        "}"
        );

    qh_lab->addWidget(send_lab1);
    qh_lab->addWidget(send_line);
    qh_lab->addWidget(send_lab2);

    qh_sendBut->addWidget(file_in);
    qh_sendBut->addWidget(file_clear);



    qv_send->addWidget(cb5);
    qv_send->addWidget(cb6);
    qv_send->addWidget(cb7);
    qv_send->addWidget(cb8);
    qv_send->addWidget(cb9);
    qv_send->addLayout(qh_lab);
    qv_send->addLayout(qh_sendBut);


    //将规划好的部件加入容器
    sendBox->setLayout(qv_send);



    /*=========================================串口数据接收========================================*/
    qh_text = new QHBoxLayout;
    qv_text = new QVBoxLayout;

    cmd_send = new QPushButton(QString("发送"), this);
    cmd_send->setFixedHeight(50);

    text_show = new QTextEdit(this);
    text_send = new QTextEdit(this);
    text_send->setFixedHeight(50);

    qh_text->addWidget(text_send);
    qh_text->addWidget(cmd_send);

    qv_text->addWidget(text_show);
    qv_text->addLayout(qh_text);

    //添加到容器
    msgBox->setLayout(qv_text);
    msgBox->setFixedWidth(600);


    /*=========================================容器布局设置========================================*/
    setWindowTitle(QString("串口调试助手（CI精装版V3.8.1）"));
    //图标
    QIcon ic(QString(":/new/prefix1/usart.jpg"));
    setWindowIcon(ic);

    main_layout = new QVBoxLayout;
    qv_box->addWidget(usartBox);
    qv_box->addWidget(recvBox);
    qv_box->addWidget(sendBox);

    qh_box->addLayout(qv_box);
    qh_box->addWidget(msgBox);

    main->setLayout(qh_box);
    main_layout->addWidget(main);
    main_layout->setStretchFactor(main, 1);

    this->setLayout(main_layout);
}

UsartToolWidget::~UsartToolWidget()
{
    delete ui;
}
